// JavaScript Document
//将图片加载到指定位置
				
			function change(file){
				document.getElementById("imgg1").style.display="block";
				document.getElementById("imgg2").style.display="block";
				document.getElementById("imgg3").style.display="block";
				document.getElementById("myvideo").style.display="none";
				document.getElementById("mycanvas").style.display="none";
			    // Get a reference to the fileList得到一个参考的文件列表
			    var files = !!file.files ? file.files : [];
			    // If no files were selected, or no FileReader support, return如果没有选中文件，或任何有支持，回报
			    if (!files.length || !window.FileReader) return;
			
			        // Create a new instance of the FileReader创建一个新实例的有
			        var reader = new FileReader();
			  
			        // Read the local file as a DataURL
			        reader.readAsDataURL(files[0]);
			  
			        // When loaded, set image data as background of div
					var img=$('#imgg1');
			        reader.onloadend = function(){
					img.attr("src",this.result);
					$("#imgg2").attr("src",this.result);
					$("#imgg3").attr("src",this.result);
        
     			 }
		}
		
		
		
		/* var base64Data ;*/
								//调取本地摄像头
								var snapPhoto = document.getElementById("snapPhoto");
								var paizhao = document.getElementById("paizhao");
								var canvas = document.getElementById("mycanvas");
								var context = canvas.getContext("2d");
								
								var video = document.getElementById("myvideo");
								paizhao.addEventListener("click" , function(){
									navigator.getMedia = (navigator.getUserMedia ||
															navigator.webkitGetUserMedia||
															navigator.mozGetUserMedia||
															navigator.msGetUserMedia);	
									navigator.getMedia(
										{
											video:true,
											audio:false,	
										},
										function(localMediaStream){
											video.src = window.URL.createObjectURL(localMediaStream);
											
											video.onloademetadata = function(e){
													
											}	
										},
										function(err){
											console.log("The following error occured: "+err);	
										}
									);
									document.getElementById("myvideo").style.display="block";
									document.getElementById("mycanvas").style.display="block";
									document.getElementById("imgg1").style.display="none";
									document.getElementById("imgg2").style.display="none";
									document.getElementById("imgg3").style.display="none";
								});
								
								
								snapPhoto.addEventListener("click",function(){
									document.getElementById("mycanvas").style.display="block";
									document.getElementById("imgg1").style.display="none";
									document.getElementById("imgg2").style.display="none";
									document.getElementById("imgg3").style.display="none";
									
									//document.getElementById("mycanvas").style.display="none";
									
									context.drawImage(video,0,0,640,480);
									document.getElementById("myvideo").style.display="none";
								})
							
							//将数据流转化为图片
							function convertCanvasToImage(canvas) {
								var image = new Image();
								image.src = canvas.toDataURL("image/png");
								return image;
							}
								/*//以下开始编 数据
								
                                  var imgData = canvass.toDataURL();
								  
								//将图像转换为base64数据
              					 base64Data = imgData.substr(22);
								//在前端截取22位之后的字符串作为图像数据
								 
								//图片加载到指定位置*/
								
									
									//获取路劲
									/*var result=convertCanvasToImage(canvass);
									document.getElementById("imgg1").style.src=result;
									document.getElementById("imgg2").style.src=result;
									document.getElementById("imgg3").style.src=result;
									
									var img=$('#imgg1');
									img.attr("src",this.result);
									$("#imgg2").attr("src",this.result);
									$("#imgg3").attr("src",this.result);*/
								
							
							/*function convertCanvasToImage(canvas) {
								var image = new Image();
								image.src = canvas.toDataURL("image/png");
								return image;
							}	*/
							
							
							
							//<!--以下js代码为控制剪切技术-->
            
    				selectrate=1;
    				rate=1;
					function preview(img, selection) {
						if (!selection.width || !selection.height)
							return;
							//此处是用来利用剪切技术
						var scaleX =  $(".ph1_bg2").width() / selection.width;
						var scaleY =  $(".ph1_bg2").height() / selection.height;
						var scaleX1 =  $(".ph1_bg3").width() / selection.width;
						var scaleY1 =  $(".ph1_bg3").height() / selection.height;
						$('.ph1_bg2 img').css({
							width : Math.round(scaleX *  $("#imgg1").width()),
							height : Math.round(scaleY * $("#imgg1").height()),
							marginLeft : -Math.round(scaleX * selection.x1),
							marginTop : -Math.round(scaleY * selection.y1)
						});
							
						$('.ph1_bg3 img').css({
							width : Math.round(scaleX1 *  $("#imgg1").width()),
							height : Math.round(scaleY1 * $("#imgg1").height()),
							marginLeft : -Math.round(scaleX1 * selection.x1),
							marginTop : -Math.round(scaleY1 * selection.y1)
						});
							
					}
						
					$(function() {
						init();
					});
					function init(){
						var width=$('#imgg1').width();
						var height=$('#imgg1').height();
						$('#imgg1').imgAreaSelect({
						  aspectRatio : "1:1",
						  handles : true,
						  fadeSpeed : 200,
						  onSelectChange : preview,
						  //x1: 0, y1: 0, x2: 240, y2: 240
					});
						
				}
	
					
	//点击保存时
					function wphotoInfo(){
						var photo=
						
						$.post("mybaiheServlet?t="+new Date(),{op:'ziliaoInfo',nickname:nickname},function(data){
							data=parseInt($.trim(data));
							if(data==1){
								location.href='myphoto.html';
							}else if(data==0){
								alert("保存失败。。。。");
							}
						});

					}
							